package com.hsq.androidproject.ui.find_fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.Toast;

import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.MyGridViewStarDTAdapter;
import com.hsq.androidproject.entity.StarDynamicTmp;
import com.hsq.androidproject.entity.find.FindVideos;
import com.hsq.androidproject.utils.OkHttpClientManager;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class ElevenFragment extends Fragment {

    private GridView mGridViewOne;

    public ElevenFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_eleven, container, false);
        Bundle bundle = getArguments();
        int id = bundle.getInt("cid");           //////根据id  path
        String path = "http://api.hanju.koudaibaobao.com/api/cate/videos?cid=" + id + "&offset=0&count=20";
        mGridViewOne = (GridView) view.findViewById(R.id.mGridViewOne);
        mGridViewOne.setFocusable(false);

         /*推荐*/
        OkHttpClientManager.getAsyn(path, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
               /* HotStars hotStars = new Gson().fromJson(response, HotStars.class);
                hotStarsBeen = hotStars.getHotStars();
                adapter = new MyGridViewStartRankingAdapter(hotStarsBeen, getContext());
                mGridViewStarRanking.setAdapter(adapter);*/

                FindVideos findVideos = new Gson().fromJson(response, FindVideos.class);
                ArrayList<StarDynamicTmp> tmps = new ArrayList<>();
                for (int i = 0; i < findVideos.getVideos().size(); i++) {
                    StarDynamicTmp tmp = new StarDynamicTmp();
                    tmp.setThumb(findVideos.getVideos().get(i).getThumb());
                    tmp.setVid(findVideos.getVideos().get(i).getVid());
                    tmp.setTitle(findVideos.getVideos().get(i).getTitle());
                    for (int j = 0; j < findVideos.getVideos().get(i).getSources().size(); j++) {
                        tmp.setOffset(findVideos.getVideos().get(i).getSources().get(j).getOffset());
                        tmp.setPage(findVideos.getVideos().get(i).getSources().get(j).getPage());
                    }
                    String substring = tmp.getPage().substring(0,32);
                    if (substring.equals("http://www.bilibili.com/video/av")) {
                        tmps.add(tmp);
                    }
                }
                MyGridViewStarDTAdapter adapter = new MyGridViewStarDTAdapter(tmps, getContext());
                mGridViewOne.setAdapter(adapter);
            }
        });

        return view;
    }

}
